package designPattern.Singleton;

/*在类上加锁*/
public class Singleton04 {
    private static Singleton04 INSTANCE;
    private Singleton04(){}

    public static synchronized Singleton04 getInstance() {
        if(INSTANCE == null) {
            try {
                Thread.sleep(1);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Singleton04();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for(int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Singleton04.getInstance().hashCode())).start();
        }
    }

}
